package android.cmpe451.group7.activity;

import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import android.cmpe451.group7.activity.R;
import android.cmpe451.group7.dbrequest.SimpleCommentTask;
import android.cmpe451.group7.dbrequest.SimplePlaceTask;
import android.cmpe451.group7.dbrequest.SimpleBadgeTask;
import android.cmpe451.group7.dbrequest.WebServiceResponse;
import android.cmpe451.group7.entity.Comment;
import android.cmpe451.group7.entity.Place;
import android.cmpe451.group7.entity.Badge;

import android.app.Activity;
import android.cmpe451.group7.util.GlobalVariables;
import android.cmpe451.group7.util.SessionManager;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;


/**
 * Creates user profile page with user's name, personal info, comments,
 * favorite places and badges.
 *
 */
public class UserProfileActivity extends Activity {
	SessionManager session;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setting default screen to login.xml
		setContentView(R.layout.user_profile);
		session = new SessionManager(getApplicationContext());
		session.checkLogin();

		HashMap<String, String> user = session.getUserDetails();
		String name = user.get(SessionManager.KEY_NAME);
		String email = user.get(SessionManager.KEY_EMAIL);
		
		HashMap<String, String> userIdDetails = session.getUserIdDetails();
		String userID = userIdDetails.get(SessionManager.KEY_USERID);
		
		HashMap<String, String> userOtherDetails= session.getUserOtherDetails();
		String surname = userOtherDetails.get(SessionManager.KEY_SURNAME);
		String aboutMe = userOtherDetails.get(SessionManager.KEY_ABOUTME);
		
		// set user name and email
		TextView usernameTextView = (TextView) findViewById(R.id.userNameText);
		TextView emailTextView = (TextView) findViewById(R.id.emailText);
		usernameTextView.setText(name + " " + surname);
		emailTextView.setText(email);
		
		// set user "about me"
		EditText aboutMeText = (EditText) findViewById(R.id.aboutMeText);
		aboutMeText.setText(aboutMe);
		
		//gets last 5 comments of the user.
		getUserComments(userID);
		
		//gets last 5 liked places of the user.
		getLikedPlaces(userID);
		
		//gets last 5 badges of the user.
		getBadges(userID);
	}
	
	/**
	 * Gets last 5 comments of the user.
	 * @param userID
	 */
	public void getUserComments(String userID){
		String url = String
				.format(GlobalVariables.get_comments_URL,userID);
		WebServiceResponse<Comment> r = null;
		TextView comment1 = (TextView) findViewById(R.id.comment1);
		TextView comment2 = (TextView) findViewById(R.id.comment2);
		TextView comment3 = (TextView) findViewById(R.id.comment3);
		TextView comment4 = (TextView) findViewById(R.id.comment4);
		TextView comment5 = (TextView) findViewById(R.id.comment5);
		try {
			r = new SimpleCommentTask().execute(url).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		if (r != null) {
			int count=1;
			int last = r.count;
			for (Comment d : r.data) {
				//int textViewID = Integer.parseInt("comment"+count);
				if(count==last) comment1.setText(d.getComment());
				else if(count==(last-1)) comment2.setText(d.getComment());
				else if(count==(last-2)) comment3.setText(d.getComment());
				else if(count==(last-3)) comment4.setText(d.getComment());
				else if(count==(last-4)) comment5.setText(d.getComment());
				else d.getComment();
				count++;
			}
		}
		
	}
	
	/**
	 * Gets last 5 liked places of the user.
	 * @param userID
	 */
	public void getLikedPlaces(String userID){
		String url = String
				.format(GlobalVariables.get_likedPlaces_URL,userID);
		WebServiceResponse<Place> r = null;
		TextView place1 = (TextView) findViewById(R.id.likedPlace1);
		TextView place2 = (TextView) findViewById(R.id.likedPlace2);
		TextView place3 = (TextView) findViewById(R.id.likedPlace3);
		TextView place4 = (TextView) findViewById(R.id.likedPlace4);
		TextView place5 = (TextView) findViewById(R.id.likedPlace5);
		try {
			r = new SimplePlaceTask().execute(url).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		if (r != null) {
			int count=1;
			int last = r.count;
			for (Place d : r.data) {
				if(count==last) place1.setText(d.getPlaceName());
				else if(count==(last-1)) place2.setText(d.getPlaceName());
				else if(count==(last-2)) place3.setText(d.getPlaceName());
				else if(count==(last-3)) place4.setText(d.getPlaceName());
				else if(count==(last-4)) place5.setText(d.getPlaceName());
				else d.getPlaceName();
				count++;
			}
		}
		
	}
	
	/**
	 * Gets last 5 badges of the user.
	 * @param userID
	 */
	public void getBadges(String userID){
		String url = String
				.format(GlobalVariables.get_badges_URL,userID);
		WebServiceResponse<Badge> r = null;
		TextView badge1 = (TextView) findViewById(R.id.badge1);
		TextView badge2 = (TextView) findViewById(R.id.badge2);
		TextView badge3 = (TextView) findViewById(R.id.badge3);
		TextView badge4 = (TextView) findViewById(R.id.badge4);
		TextView badge5 = (TextView) findViewById(R.id.badge5);
		try {
			r = new SimpleBadgeTask().execute(url).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		if (r != null) {
			int count=1;
			int last = r.count;
			for (Badge d : r.data) {
				if(count==last) badge1.setText(d.getBadgeName());
				else if(count==(last-1)) badge2.setText(d.getBadgeName());
				else if(count==(last-2)) badge3.setText(d.getBadgeName());
				else if(count==(last-3)) badge4.setText(d.getBadgeName());
				else if(count==(last-4)) badge5.setText(d.getBadgeName());
				else d.getBadgeName();
				count++;
			}
		}
		
	}
}
